package com.xgry.bpm.api.business.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.author.dao.TBAuthorDao;
import com.xgry.bpm.api.business.author.entity.TBAuthor;
import com.xgry.bpm.api.business.author.form.TBAuthorForm;
import com.xgry.bpm.api.business.author.service.TBAuthorService;
import com.xgry.bpm.api.business.author.vo.TBAuthorVO;
import com.xgry.bpm.api.business.author.vo.TreeDataCheckVo;
import com.xgry.bpm.common.utils.MiniUITreeCheckUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;


/**
 * <p>
 * TBAuthor服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-15
 */
@Service
public class TBAuthorServiceImpl extends ServiceImpl<TBAuthorDao, TBAuthor> implements TBAuthorService {

    @Override
    public int save(TBAuthorForm record) {
        TBAuthor data = this.convertDO(record);
        return baseMapper.insert(data);
    }

    @Override
    public int updateById(TBAuthorForm record) {
        //修改时间
        TBAuthor data = this.convertDO(record);
        return baseMapper.updateById(data);
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public TBAuthorVO selectById(String id) {
        TBAuthor data = baseMapper.selectById(id);
        TBAuthorVO vo = new TBAuthorVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<TBAuthorVO> selectAll(TBAuthorForm record) {
        TBAuthor data = this.convertDO(record);
        QueryWrapper<TBAuthor> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBAuthor> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<TBAuthorVO> selectPage(TBAuthorForm record) {
        // form -> do 转换
        TBAuthor data = this.convertDO(record);
        // 分页数据设置
        Page<TBAuthor> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<TBAuthor> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<TBAuthor> list = baseMapper.selectPage(page, queryWrapper);
        IPage<TBAuthorVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<TBAuthor> getPage(IPage<TBAuthor> page, QueryWrapper<TBAuthor> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<TreeDataCheckVo> getAuthorData(Integer roleId) {
        return baseMapper.getAuthorTree(roleId);
    }

    @Override
    public List<TreeDataCheckVo> getAuthorDataByRoleIds(List<Integer> roleIds) {
        List<TreeDataCheckVo> treeDataCheckVos = new ArrayList<>(roleIds.size());
        for (Integer roleId : roleIds) {
            treeDataCheckVos.addAll(this.getAuthorData(roleId));
        }
        return treeDataCheckVos;
    }

    @Override
    public List<MiniUITreeCheckUtil> toAuthorTree(List<TreeDataCheckVo> list) {
        List<MiniUITreeCheckUtil> treeData = new ArrayList<>();
        for (TreeDataCheckVo treeDataCheckVo : list) {
            treeData.add(new MiniUITreeCheckUtil(treeDataCheckVo.getId(),treeDataCheckVo.getPid(),
                    treeDataCheckVo.getText(),treeDataCheckVo.getIconCls(),treeDataCheckVo.getUrl(),treeDataCheckVo.getChecked()));
        }
        treeData = treeData.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MiniUITreeCheckUtil::getId))), ArrayList::new));
        return MiniUITreeCheckUtil.getTreeResult(treeData);
    }


    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private TBAuthor convertDO(TBAuthorForm form) {
        TBAuthor data = new TBAuthor();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<TBAuthorVO> convert(List<TBAuthor> list) {
        List<TBAuthorVO> tBAuthorList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return tBAuthorList;
        }
        for (TBAuthor source : list) {
            TBAuthorVO target = new TBAuthorVO();
            BeanUtils.copyProperties(source, target);
            tBAuthorList.add(target);
        }
        return tBAuthorList;
    }

}
